// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include <Media.hpp>

#include <cstring>

const uint8_t Media::ID_MOVIE   = 0x01;
const uint8_t Media::ID_TVMOVIE = 0x02;
const uint8_t Media::ID_TVSERIE = 0x04;
const uint8_t Media::ID_VGAME   = 0x08;
const uint8_t Media::ID_VIDEO   = 0x10;
const uint8_t Media::ID_UNKNOWN = 0x80;
const uint8_t Media::ID_ALL     = 0xFF;

const char * Media::TAG_MOVIE   = "Movie";
const char * Media::TAG_TVMOVIE = "TV Movie";
const char * Media::TAG_TVSERIE = "TV Serie";
const char * Media::TAG_VGAME   = "Video Game";
const char * Media::TAG_VIDEO   = "Video";
const char * Media::TAG_UNKNOWN = "Unknown";
const char * Media::TAG_ALL     = "All";

Media::Media() : _media(ID_UNKNOWN) 
{
}

Media::Media(const uint8_t media) : _media(media)
{
}

Media::Media(const char * media)
{
  if ( !strcasecmp(media, TAG_MOVIE) )
    _media = ID_MOVIE;
  else if ( !strcasecmp(media, TAG_TVSERIE) )
    _media = ID_TVSERIE;
  else if ( !strcasecmp(media, TAG_TVMOVIE) )
    _media = ID_TVMOVIE;
  else if ( !strcasecmp(media, TAG_VGAME) )
    _media = ID_VGAME;
  else if ( !strcasecmp(media, TAG_VIDEO) )
    _media = ID_VIDEO;
  else if ( !strcasecmp(media, TAG_ALL) )
    _media = ID_ALL;
  else 
    _media = ID_UNKNOWN;
}

Media& Media::operator = (const uint8_t media)
{
  _media = media;
  return *this;
}

bool Media::operator == (const Media& media) const
{
  return ( media._media == _media );
}

bool Media::operator == (const uint8_t media) const
{
  return ( media == _media );
}

bool Media::operator != (const Media& media) const
{
  return ( media._media != _media );
}

bool Media::operator != (const uint8_t media) const
{
  return ( media != _media );
}

Media::operator uint8_t () const
{
  return _media;
}

Media::operator const char * () const
{
  switch(_media) {
  case ID_MOVIE:
    return TAG_MOVIE;
  case ID_TVSERIE:
    return TAG_TVSERIE;
  case ID_TVMOVIE:
    return TAG_TVMOVIE;
  case ID_VGAME:
    return TAG_VGAME;
  case ID_VIDEO:
    return TAG_VIDEO;
  case ID_ALL:
    return TAG_ALL;
  default:
    return TAG_UNKNOWN;
  }
}

bool Media::mask(const Media& media_a, const Media& media_b)
{
  /* Bit-wise AND */
  return ( (media_a._media & media_b._media) != 0x00 );
}



